package com.ray.dao;

import com.ray.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao
{

    class MyRowMapper implements RowMapper<User>
    {
        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException
        {
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setGender(resultSet.getInt("gender"));
            return user;
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public User getUser(int id)
    {

        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = classPathXmlApplicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        User user = (User) jdbcTemplate.queryForObject("select * from test.user where id = ?", BeanPropertyRowMapper.newInstance(User.class), 1);

        System.out.println(user.toString());
        return user;
    }

    /**
     * @return
     */
    @Override
    public List<User> getUserList()
    {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = classPathXmlApplicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        List<User> userList = (List<User>) jdbcTemplate.query("select * from test.user ", new MyRowMapper());

        // 以下语句是使用内置方式，直接进行  约定  映射，不需要自己定义映射关系。
        // List<User> userList = (List<User>) jdbcTemplate.query("select * from test.user ", new BeanPropertyRowMapper<>(User.class));
        System.out.println(userList);

        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet("select * from test.user");
        while (sqlRowSet.next())
        {
            System.out.println(sqlRowSet.getString("name"));
            ;
        }

        return userList;
    }

    /**
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user)
    {
        return 0;
    }

    /**
     * @param user
     * @return
     */
    @Override
    public int addUser(User user)
    {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = classPathXmlApplicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        String sql = "INSERT INTO TEST.USER (NAME, GENDER) VALUES (?,?)";
        int update = jdbcTemplate.update(sql, user.getName(), user.getGender());
        System.out.println(update);
        return update;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int deleteUser(int id)
    {
        return 0;
    }
}
